package com.example.home.logistics;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.example.home.R;
import com.example.home.logistics.activity.LogisticsSearchDetailActivity;
import com.example.home.logistics.adapter.LogisticsBannerAdapter;
import com.example.home.logistics.adapter.LogisticsCardDataAdapter;
import com.example.home.logistics.adapter.LogisticsDetailAdapter;
import com.example.home.logistics.bean.LogisticsBannerBean;
import com.example.home.logistics.bean.LogisticsCardDataBean;
import com.example.home.logistics.bean.LogisticsSearchDataBean;
import com.example.home.tool.OKHttpTool;
import com.google.gson.Gson;

import java.util.Timer;
import java.util.TimerTask;

public class LogisticsMainActivity extends AppCompatActivity {
    private ImageView back;
    private SearchView houseSearch;
    private ViewPager vpBanner;
    private RecyclerView rcvCard;
    private RecyclerView rcvList;

    private Timer timer;
    private int currentPage = 0;
    private final long DELAY_MS = 1000;
    private final long PERIOD_MS = 3000;
    private LogisticsBannerAdapter logisticsBannerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logistics_main);
        initView();
        //轮播图
        getBanner();
        startAutoScroll();

        //宫格类目入口
        getCardData();

        //其它快递公司列表
        getRcvList();


    }

    private void getRcvList() {
        OKHttpTool.doNetRequest("prod-api/api/logistics-inquiry/logistics_company/list",
                OKHttpTool.GET,
                LogisticsCardDataBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        LogisticsCardDataBean logisticsBannerBean = (LogisticsCardDataBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindList(logisticsBannerBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                }
        );
    }

    private void bindList(LogisticsCardDataBean logisticsBannerBean) {
        LogisticsDetailAdapter logisticsDetailAdapter = new LogisticsDetailAdapter(LogisticsMainActivity.this,logisticsBannerBean.getData());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(LogisticsMainActivity.this);
        rcvList.setAdapter(logisticsDetailAdapter);
        rcvList.setLayoutManager(linearLayoutManager);
    }

    private void getCardData() {
        OKHttpTool.doNetRequest("prod-api/api/logistics-inquiry/logistics_company/list",
                OKHttpTool.GET,
                LogisticsCardDataBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        LogisticsCardDataBean logisticsBannerBean = (LogisticsCardDataBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindCardshow(logisticsBannerBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                }
        );
    }

    private void bindCardshow(LogisticsCardDataBean logisticsBannerBean) {
        LogisticsCardDataAdapter logisticsCardDataAdapter = new LogisticsCardDataAdapter(LogisticsMainActivity.this,logisticsBannerBean.getData());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(LogisticsMainActivity.this, 4);
        rcvCard.setLayoutManager(gridLayoutManager);
        rcvCard.setAdapter(logisticsCardDataAdapter);
    }


    private void getBanner() {
        OKHttpTool.doNetRequest("prod-api/api/logistics-inquiry/ad-banner/list",
                OKHttpTool.GET,
                LogisticsBannerBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        LogisticsBannerBean logisticsBannerBean = (LogisticsBannerBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindBannershow(logisticsBannerBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                }
        );
    }

    private void bindBannershow(LogisticsBannerBean logisticsBannerBean) {
        ViewPager viewPager = findViewById(R.id.vp_banner);
        logisticsBannerAdapter = new LogisticsBannerAdapter(LogisticsMainActivity.this, logisticsBannerBean.getData());
        viewPager.setAdapter(logisticsBannerAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                currentPage = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void startAutoScroll() {
        final Handler handler = new Handler();
        final Runnable update = new Runnable() {
            @Override
            public void run() {
                if (currentPage == logisticsBannerAdapter.getCount()) {
                    currentPage = 0;
                }
                vpBanner.setCurrentItem(currentPage++, true);
                ;
            }
        };
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.post(update);
            }
        }, DELAY_MS, PERIOD_MS);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
        }
    }
    private void initView() {
        back = findViewById(R.id.back);
        houseSearch = findViewById(R.id.house_search);
        vpBanner = findViewById(R.id.vp_banner);
        rcvCard = findViewById(R.id.rcv_card);
        rcvList = findViewById(R.id.rcv_list);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogisticsMainActivity.this.finish();
            }
        });

        //搜索功能  //ST0000001
        houseSearch.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                // 在用户提交查询时调用
                performSearch(query);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
    }

    private void performSearch(String query) {
        OKHttpTool.doNetRequest("prod-api/api/logistics-inquiry/logistics_info/"+query,
                OKHttpTool.GET,
                LogisticsSearchDataBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        LogisticsSearchDataBean logisticsSearchDataBean = (LogisticsSearchDataBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                // 直接使用返回的对象进行匹配
                                if (logisticsSearchDataBean.getData() != null && logisticsSearchDataBean.getData().getInfoNo().toLowerCase().contains(query.toLowerCase())) {
                                    // 匹配成功，处理跳转逻辑
                                    navigateToDetailActivity(logisticsSearchDataBean.getData());
                                } else {
                                    // 处理无匹配项的情况
                                    Toast.makeText(LogisticsMainActivity.this,"暂无结果",Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void navigateToDetailActivity(LogisticsSearchDataBean.DataBean dataBean) {
        // 根据匹配项跳转到相应的 Activity
        Intent intent = new Intent(LogisticsMainActivity.this, LogisticsSearchDetailActivity.class);
        Gson gson = new Gson();
        intent.putExtra("seaid", gson.toJson(dataBean));
        // 添加其他需要传递的数据
        startActivity(intent);
    }
}